package com.juice.storage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.juice.model.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.UUID;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/4/11 11:30 下午
 */

@SpringBootTest
public class BookDaoTestCase {

    @Autowired
    private BookDao bookDao;

    @Test
    public void bookGetByIdTest() {
        Book book = bookDao.selectById(1L);
        System.out.println(book);
    }

    @Test
    public void testGetAll() {
        List<Book> books = bookDao.selectList(null);
        System.out.println(books);
    }

    @Test
    public void testSave() {
        System.out.println(UUID.randomUUID());
        Book book = new Book(4L, "音乐", "吉他", "灵魂曲");
        Book book2 = new Book(5L, "音乐", "钢琴", "姬野哪");
        bookDao.insert(book);
        bookDao.insert(book2);
    }

    @Test
    public void testUpdate() {
        Book book = new Book(4L, "数学", "线性代数", null);
        bookDao.updateById(book);
    }

    @Test
    public void testDelete() {
        bookDao.deleteById(4);
    }


    @Test
    public void getByPage() {
        Page<Book> bookPage = Page.of(1,2);
        bookPage = bookDao.selectPage(bookPage, null);
        bookPage.getRecords().forEach(System.out::println);

        bookPage = Page.of(2,2);
        bookDao.selectPage(bookPage, null);
        bookPage.getRecords().forEach(System.out::println);
    }


    @Test
    public void testGetByCondition() {
        QueryWrapper<Book> qw = new QueryWrapper<>();
        qw.like("name", "JAVA");
        qw.gt("id", 2);
        List<Book> books = bookDao.selectList(qw);
        System.out.println(books);
    }

    @Test
    public void testGetByCondition2() {
        LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();
        String name = "java";
        qw.like(name != null, Book::getName, name);
        List<Book> books = bookDao.selectList(qw);
        System.out.println(books);
    }

}
